home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 1.iso / dist / fw_apache2.idb / usr / freeware / apache2 / include / apr_anylock.h.z / apr_anylock.h
C/C++ Source or Header  |  2002-07-08  |  6KB  |  161 lines

  1. /* ====================================================================
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  *
  26.  * 4. The names "Apache" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation.  For more
  51.  * information on the Apache Software Foundation, please see
  52.  * <http://www.apache.org/>.
  53.  */
  54.  
  55. /**
  56.  * @file apr_anylock.h
  57.  * @brief APR-Util transparent any lock flavor wrapper
  58.  */
  59. #ifndef APR_ANYLOCK_H
  60. #define APR_ANYLOCK_H
  61.  
  62. #include "apr_proc_mutex.h"
  63. #include "apr_thread_mutex.h"
  64. #include "apr_thread_rwlock.h"
  65.  
  66. typedef struct apr_anylock_t {
  67.     enum tm_lock {
  68.         apr_anylock_none,
  69.         apr_anylock_procmutex,
  70.         apr_anylock_threadmutex,
  71.         apr_anylock_readlock,
  72.         apr_anylock_writelock
  73.     } type;
  74.     union apr_anylock_u_t {
  75.         apr_proc_mutex_t *pm;
  76. #if APR_HAS_THREADS
  77.         apr_thread_mutex_t *tm;
  78.         apr_thread_rwlock_t *rw;
  79. #endif
  80.     } lock;
  81. } apr_anylock_t;
  82.  
  83. #if APR_HAS_THREADS
  84.  
  85. #define APR_ANYLOCK_LOCK(lck)                \
  86.     (((lck)->type == apr_anylock_none)         \
  87.       ? APR_SUCCESS                              \
  88.       : (((lck)->type == apr_anylock_threadmutex)  \
  89.           ? apr_thread_mutex_lock((lck)->lock.tm)    \
  90.           : (((lck)->type == apr_anylock_procmutex)    \
  91.               ? apr_proc_mutex_lock((lck)->lock.pm)      \
  92.               : (((lck)->type == apr_anylock_readlock)     \
  93.                   ? apr_thread_rwlock_rdlock((lck)->lock.rw) \
  94.                   : (((lck)->type == apr_anylock_writelock)    \
  95.                       ? apr_thread_rwlock_wrlock((lck)->lock.rw) \
  96.                       : APR_EINVAL)))))
  97.  
  98. #else /* APR_HAS_THREADS */
  99.  
  100. #define APR_ANYLOCK_LOCK(lck)                \
  101.     (((lck)->type == apr_anylock_none)         \
  102.       ? APR_SUCCESS                              \
  103.           : (((lck)->type == apr_anylock_procmutex)    \
  104.               ? apr_proc_mutex_lock((lck)->lock.pm)      \
  105.                       : APR_EINVAL))
  106.  
  107. #endif /* APR_HAS_THREADS */
  108.  
  109. #if APR_HAS_THREADS
  110.  
  111. #define APR_ANYLOCK_TRYLOCK(lck)                \
  112.     (((lck)->type == apr_anylock_none)            \
  113.       ? APR_SUCCESS                                 \
  114.       : (((lck)->type == apr_anylock_threadmutex)     \
  115.           ? apr_thread_mutex_trylock((lck)->lock.tm)    \
  116.           : (((lck)->type == apr_anylock_procmutex)       \
  117.               ? apr_proc_mutex_trylock((lck)->lock.pm)      \
  118.               : (((lck)->type == apr_anylock_readlock)        \
  119.                   ? apr_thread_rwlock_tryrdlock((lck)->lock.rw) \
  120.                   : (((lck)->type == apr_anylock_writelock)       \
  121.                       ? apr_thread_rwlock_trywrlock((lck)->lock.rw) \
  122.                           : APR_EINVAL)))))
  123.  
  124. #else /* APR_HAS_THREADS */
  125.  
  126. #define APR_ANYLOCK_TRYLOCK(lck)                \
  127.     (((lck)->type == apr_anylock_none)            \
  128.       ? APR_SUCCESS                                 \
  129.           : (((lck)->type == apr_anylock_procmutex)       \
  130.               ? apr_proc_mutex_trylock((lck)->lock.pm)      \
  131.                           : APR_EINVAL))
  132.  
  133. #endif /* APR_HAS_THREADS */
  134.  
  135. #if APR_HAS_THREADS
  136.  
  137. #define APR_ANYLOCK_UNLOCK(lck)              \
  138.     (((lck)->type == apr_anylock_none)         \
  139.       ? APR_SUCCESS                              \
  140.       : (((lck)->type == apr_anylock_threadmutex)  \
  141.           ? apr_thread_mutex_unlock((lck)->lock.tm)  \
  142.           : (((lck)->type == apr_anylock_procmutex)    \
  143.               ? apr_proc_mutex_unlock((lck)->lock.pm)    \
  144.               : ((((lck)->type == apr_anylock_readlock) || \
  145.                   ((lck)->type == apr_anylock_writelock))    \
  146.                   ? apr_thread_rwlock_unlock((lck)->lock.rw)   \
  147.                       : APR_EINVAL))))
  148.  
  149. #else /* APR_HAS_THREADS */
  150.  
  151. #define APR_ANYLOCK_UNLOCK(lck)              \
  152.     (((lck)->type == apr_anylock_none)         \
  153.       ? APR_SUCCESS                              \
  154.           : (((lck)->type == apr_anylock_procmutex)    \
  155.               ? apr_proc_mutex_unlock((lck)->lock.pm)    \
  156.                       : APR_EINVAL))
  157.  
  158. #endif /* APR_HAS_THREADS */
  159.  
  160. #endif /* !APR_ANYLOCK_H */
  161.